package com.blb.friendsO.servlet;

import com.blb.friendsO.entity.TComment;
import com.blb.friendsO.entity.TFriends;
import com.blb.friendsO.entity.TGood;
import com.blb.friendsO.entity.Result;
import com.blb.friendsO.entity.TFriends;
import com.blb.friendsO.service.impl.FriendsServiceImpl;
import com.blb.friendsO.utils.ApplicationContextUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.time.LocalDateTime;

@WebServlet(value = "/friends/*", loadOnStartup = 1)
public class FriendsServlet extends BaseServlet{

    private FriendsServiceImpl friendsService = (FriendsServiceImpl) ApplicationContextUtil.getBean(FriendsServiceImpl.class);

    /**
     * 登录
     * @param req
     * @param resp
     */
    public void login(HttpServletRequest req, HttpServletResponse resp){

    }

    /**
     * 注册
     * @param req
     * @param resp
     */
    public void register(HttpServletRequest req, HttpServletResponse resp){

    }

    /**
     * 添加
     * @param req
     * @param resp
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            TFriends friends1 = getEntity(TFriends.class);
            LocalDateTime time = LocalDateTime.now();
            friends1.setTime(time);
            friends1.setUserId(1);
            System.out.println(friends1);
            friendsService.add(friends1);
            writeJson(Result.ok("发表成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("网卡了，发表失败！"));
        }

    }

    //页面
    public void index(HttpServletRequest req, HttpServletResponse resp){
        try {
            //获取所有动态
            List<TFriends> friendsList = friendsService.getFriends();
            //获取点赞
            List<TGood> goodList = friendsService.getGood();
            //获取评论
            List<TComment> commentList = friendsService.getComment();

            req.setAttribute("friendsList", friendsList);
            req.setAttribute("goodList", goodList);
            req.setAttribute("commentList", commentList);
            req.getRequestDispatcher("/friends/view/index").forward(req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除动态
     *
     * @param req
     * @param resp
     * @throws IOException
     */

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            friendsService.deleteById(Integer.parseInt(id));
            writeJson(Result.ok("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("删除失败：" + e.getMessage()));
        }
    }

    /**
     * 点赞
     * @param req
     * @param resp
     */
    public void good(HttpServletRequest req, HttpServletResponse resp) throws InstantiationException, IllegalAccessException {
        String friendsID = req.getParameter("friendsID");
        int userId = 1;
        try {
            friendsService.addGood(userId,Integer.parseInt(friendsID));
            resp.sendRedirect("/friends/index");
        } catch (Exception e) {

            e.printStackTrace();
        }

    }


}
